package com.nsgf.library.ui.page;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.binding_recyclerview.adapter.BaseDataBindingAdapter;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.ClockInBean;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.databinding.ActivityProjectSearchBinding;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.adapter.HomeSearchProjectListAdapter;

import java.util.List;

/**
 * 搜索水库
 */
public class SearchProjectActivity extends BaseActivity {

    StateHolder mState;

    private ActivityProjectSearchBinding mBinding;

    private HomeSearchProjectListAdapter mAdapter;

    private AccountRequester mAccountRequester;

    @Override
    protected void initViewModel() {
        mState = getActivityScopeViewModel(StateHolder.class);

    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        mAdapter = new HomeSearchProjectListAdapter(this);
        return new DataBindingConfig(R.layout.activity_project_search, BR.vm, mState)
                .addBindingParam(BR.adapter, mAdapter)
                .addBindingParam(BR.click, new ClickProxy());
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);

        mBinding = (ActivityProjectSearchBinding) getBinding();

        getDatas();


        mAccountRequester.getProjectList().observe(this, listResponseStatus -> {
            if (listResponseStatus.getResponseCode() == 200) {
                mState.projectList.set(listResponseStatus.getData());
                if (listResponseStatus.getData().size() == 0) {
                    mBinding.notdata.setVisibility(View.VISIBLE);
                } else {
                    mBinding.notdata.setVisibility(View.GONE);
                }
            } else {
                showToast(listResponseStatus.getMsg());
                mBinding.notdata.setVisibility(View.VISIBLE);
            }
        });
        mAdapter.setOnItemClickListener((viewId, item, position) -> {

            ClockInBean.DataBean.LateAndEarlyListBean info = new ClockInBean.DataBean.LateAndEarlyListBean();

            String[] area = item.getArea().split(",");

            info.setDeviceName(item.getFacilityName());
            info.setFacilityName(item.getFacilityName());
            info.setLat(area[1]);
            info.setLon(area[0]);
            info.setLocation(item.getLocation());
            info.setPictureUrl(item.getPictureUrl());
            info.setProjectCode(item.getFacilityCode());
            startActivity(new Intent()
                    .putExtra("info", info)
                    .setClass(SearchProjectActivity.this,
                            ReservoirDetailsActivity.class));
        });
    }

    private void getDatas() {

        mAccountRequester.getReservoirByName(mState.searchContent.get());
    }

    public class ClickProxy {

        public void search() {
            getDatas();
        }
    }
}
